<?php
$language = empty($GLOBALS['xoopsConfig']['language']) ? 'english' : $GLOBALS['xoopsConfig']['language'];
$lang_dir = dirname(__FILE__) . '/language/';
if (file_exists($lang_file = $lang_dir . $language . '/blocks.php')
      || file_exists($lang_file = $lang_dir . 'english/blocks.php')) {
    include_once $lang_file;
}

function b_xigg_render($module_dirname, $templateFile, $templateVars = array())
{
	require dirname(__FILE__) . '/common.php';
    $template =& SabaiXOOPS::getTemplate($xigg, 'Xigg', $module_dirname);
    return $template->render($templateFile, $templateVars);
}

function b_xigg_categories($options)
{
    $block = array();
    if ($module_dirname = $options[0]) {
        require dirname(__FILE__) . '/common.php';
        $model =& $xigg->getService('Model');
        $r =& $model->getRepository('Category');
        $categories =& $r->fetchAll();
        $html = array();
        $entities = array();
        while ($category =& $categories->getNext()) {
            $parent_id = intval($category->getParentId());
            $entities[$parent_id]['children'][] =& $category;
            unset($category);
        }
        if (!empty($entities[0]['children'])) {
            foreach (array_keys($entities[0]['children']) as $i) {
                b_xigg_categories_render($html, $entities, $entities[0]['children'][$i], XOOPS_URL . '/modules/' . $module_dirname . '/index.php', ' - ');
            }
        }
        $block['content'] = implode("<br />\n", $html);
    }
    return $block;
}

function b_xigg_categories_render(&$html, $categories, &$category, $script, $prefixOrig, $prefix = '')
{
    $id = $category->getId();
    array_push($html, sprintf('%s<a href="%s">%s</a>', $prefix, Sabai_URL::get('/', array('category_id' => $id), '', $script), h($category->getLabel())));
    if (!empty($categories[$id]['children'])) {
        foreach (array_keys($categories[$id]['children']) as $i) {
            b_xigg_categories_render($html, $categories, $categories[$id]['children'][$i], $script, $prefixOrig, $prefix . $prefixOrig);
        }
    }
}

function b_xigg_categories_edit($options)
{
    $form = sprintf('<input type="hidden" name="options[]" value="%s" />', $options[0]);
	return $form;
}

function b_xigg_tag_cloud($options)
{
    $block = array();
    if ($module_dirname = $options[0]) {
        require dirname(__FILE__) . '/common.php';
        $tag_cloud =& $xigg->getService('TagCloudBuilder');
        $vars['tags'] = $tag_cloud->build($options[1], XOOPS_URL . '/modules/' . $module_dirname . '/index.php');
        $template =& SabaiXOOPS::getTemplate($xigg, 'Xigg', $module_dirname);
        $block['content'] = $template->render('xigg_xoops_tagcloud.html', $vars);
    }
    return $block;
}

function b_xigg_tag_cloud_edit($options)
{
    $form = sprintf('<input type="hidden" name="options[]" value="%s" />', $options[0]);
    $form .= sprintf('<dl><dt>%s</dt><dd><input type="text" name="options[]" value="%d" /></dd>', _MB_XIGG_TAGS_NUM, $options[1]);
	return $form;
}

function b_xigg_recent_nodes($options)
{
    $block = array();
    if ($module_dirname = $options[0]) {
        require dirname(__FILE__) . '/common.php';
        $model =& $xigg->getService('Model');
        $r =& $model->getRepository('Node');
        $nodes =& $r->fetchByCriteria(Sabai_Model_Criteria::createValue('node_hidden', 0), $options[1], 0, array('node_priority', 'node_published'), array('DESC', 'DESC'));
        if ($nodes->size() > 0) {
            $template =& SabaiXOOPS::getTemplate($xigg, 'Xigg', $module_dirname);
            $block['content'] = $template->render('xigg_xoops_recentnodes.html', array('nodes' => &$nodes));
        }
    }
    return $block;
}

function b_xigg_recent_nodes_edit($options)
{
    $form = sprintf('<input type="hidden" name="options[]" value="%s" />', $options[0]);
	$form .= sprintf('<dl><dt>%s</dt><dd><input type="text" name="options[]" value="%d" /></dd>', _MB_XIGG_RECENT_NUM, $options[1]);
	return $form;
}

function b_xigg_recent_comments($options)
{
    $block = array();
    if ($module_dirname = $options[0]) {
        require dirname(__FILE__) . '/common.php';
        $model =& $xigg->getService('Model');
        $r =& $model->getRepository('Comment');
        $vars['comments'] =& $r->fetchAll($options[1], 0, 'comment_created', 'DESC');
        $template =& SabaiXOOPS::getTemplate($xigg, 'Xigg', $module_dirname);
        $block['content'] = $template->render('xigg_xoops_recentcomments.html', $vars);
    }
    return $block;
}

function b_xigg_recent_comments_edit($options)
{
    $form = sprintf('<input type="hidden" name="options[]" value="%s" />', $options[0]);
	$form .= sprintf('<dl><dt>%s</dt><dd><input type="text" name="options[]" value="%d" /></dd></dl>', _MB_XIGG_COMMENTS_NUM, $options[1]);
	return $form;
}

function b_xigg_recent_trackbacks($options)
{
    $block = array();
    if ($module_dirname = $options[0]) {
        require dirname(__FILE__) . '/common.php';
        $model =& $xigg->getService('Model');
        $r =& $model->getRepository('Trackback');
        $vars['trackbacks'] =& $r->fetchAll($options[1], 0, 'trackback_created', 'DESC');
        $template =& SabaiXOOPS::getTemplate($xigg, 'Xigg', $module_dirname);
        $block['content'] = $template->render('xigg_xoops_recenttrackbacks.html', $vars);
    }
    return $block;
}

function b_xigg_recent_trackbacks_edit($options)
{
	$form = sprintf('<input type="hidden" name="options[]" value="%s" />', $options[0]);
	$form .= sprintf('<dl><dt>%s</dt><dd><input type="text" name="options[]" value="%d" /></dd></dl>', _MB_XIGG_TRACKBACKS_NUM, $options[1]);
	return $form;
}

function b_xigg_recent_votes($options)
{
    $block = array();
    if ($module_dirname = $options[0]) {
        require dirname(__FILE__) . '/common.php';
        $model =& $xigg->getService('Model');
        $r =& $model->getRepository('Vote');
        $vars['votes'] =& $r->fetchAll($options[1], 0, 'vote_created', 'DESC');
        $template =& SabaiXOOPS::getTemplate($xigg, 'Xigg', $module_dirname);
        $block['content'] = $template->render('xigg_xoops_recentvotes.html', $vars);
    }
    return $block;
}

function b_xigg_recent_votes_edit($options)
{
	$form = sprintf('<input type="hidden" name="options[]" value="%s" />', $options[0]);
	$form .= sprintf('<dl><dt>%s</dt><dd><input type="text" name="options[]" value="%d" /></dd></dl>', _MB_XIGG_VOTES_NUM, $options[1]);
	return $form;
}

function b_xigg_recent_nodes2($options)
{
    $block = array();
    if ($module_dirname = $options[0]) {
        require dirname(__FILE__) . '/common.php';
        $model =& $xigg->getService('Model');
        $r =& $model->getRepository('Node');
        $c =& Sabai_Model_Criteria::createValue('node_hidden', 0);
        $nodes =& $r->fetchByCriteria($c, $options[1], 0, array('node_priority', 'node_published'), array('DESC', 'DESC'));
        if ($nodes->size() > 0) {
            $nodes =& $nodes->with('Category'); $nodes =& $nodes->with('Tags'); $nodes =& $nodes->with('User');
            $top_nodes =& $r->fetchByCriteria($c, $options[2], 0, 'node_vote_count', 'DESC');
            $top_nodes =& $top_nodes->with('Tags'); $top_nodes =& $top_nodes->with('Category');
            $vars['nodes'] =& $nodes;
            $vars['top_nodes'] =& $top_nodes;
            $vars['xoops_script'] = XOOPS_URL . '/modules/' . $module_dirname . '/index.php';
            $vars['featured_nodes_count'] = isset($options[3]) ? intval($options[3]) : 1;
            $vars['show_screenshot'] = empty($options[4]) ? false : true;
            if (file_exists(XOOPS_ROOT_PATH . '/themes/' . $GLOBALS['xoopsConfig']['theme_set'] . '/modules/Xigg/css/screen.css')) {
                $vars['css_url'] = XOOPS_URL . '/themes/' . $GLOBALS['xoopsConfig']['theme_set'] . '/modules/Xigg/css/screen.css';
            } else {
                $vars['css_url'] = sprintf('%s/modules/%s/layouts/default/css/screen.css', XOOPS_URL, $module_dirname);
            }
            $template =& SabaiXOOPS::getTemplate($xigg, 'Xigg', $module_dirname);
            $block['content'] = $template->render('xigg_xoops_recentnodes2.html', $vars);
        }
    }
    return $block;
}

function b_xigg_recent_nodes2_edit($options)
{
    $form = sprintf('<input type="hidden" name="options[]" value="%s" />', $options[0]);
	$form .= sprintf('<dl><dt>%s</dt><dd><input type="text" name="options[]" value="%d" /></dd>', _MB_XIGG_RECENT_NUM, $options[1]);
	$form .= sprintf('<dt>%s</dt><dd><input type="text" name="options[]" value="%d" /></dd>', _MB_XIGG_TOPVOTED_NUM, $options[2]);
	$form .= sprintf('<dt>%s</dt><dd><input type="text" name="options[]" value="%d" /></dd></dl>', _MB_XIGG_RECENT_FNUM, $options[3]);
	$form .= sprintf('<dt>%s</dt><dd><input type="checkbox" name="options[]" value="1"%s /></dd></dl>', _MB_XIGG_RECENT_FSHOT, !empty($options[4]) ? ' checked="checked"' : '');
	return $form;
}